package com.swh.cloud.biz.controll;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.swh.cloud.biz.BIZServerLaunch;
import com.swh.cloud.biz.service.impl.UserService;
import com.swh.user.domain.UserEntity;

@EnableAutoConfiguration
@RestController
@RequestMapping(value = "/userService")
public class UserController {

    @Autowired
    private UserService userService ;
    
     private static int byIDCount = 0 ;
     
     private static int byUnionCount = 0 ;
    
    private static final Logger logger = LoggerFactory.getLogger(BIZServerLaunch.class);
	
    @Autowired
    void setEnvironment(Environment env) {
	logger.info(">>>> mysqldb.datasource.url from env: "+ env.getProperty("mysqldb.datasource.url"));
    }

    @HystrixCommand(fallbackMethod = "fallbackGetUserInfo",groupKey="cloud-server-UserController")
    @RequestMapping(value="/getUserInfo/{id}")
    public UserEntity getById(@PathVariable(value="id") Long id) throws InterruptedException{
    	Thread.sleep(1000);
    	logger.info("id:"+id);
		if(id == null){
			id = 1L ;
		}
		UserEntity user = userService.getById(id);
		logger.info((byIDCount++)+" 返回值:"+ReflectionToStringBuilder.toString(user));
		return user ;
    }
    
    @HystrixCommand(fallbackMethod = "fallbackGetUserByUnion",groupKey="cloud-server-UserController")
    @RequestMapping(value="/getUserByUnion")
    public List<UserEntity> getUserByUnion(Map<String, UserEntity> paraMap){
       List<UserEntity> lists = userService.getUserByUnion(paraMap);
	   for (UserEntity userEntity : lists) {
	    System.out.println(ReflectionToStringBuilder.toString(userEntity));
	   }
	   logger.info((byUnionCount++)+" 返回值个数:"+lists.size());
	  return lists ;
    }
    
  //回退方法
    @SuppressWarnings("unused")
    private UserEntity fallbackGetUserInfo(Long id) {
	 logger.info(">>>>>>>> UserController >>>>HystrixCommand fallbackMethod(fallbackGetUserInfo) handle!");
	 UserEntity user = new UserEntity();
	 user.setUserName("TestHystrixCommand->UserServer");
	 return user;
    }
    
    private List<UserEntity> fallbackGetUserByUnion(Map<String, UserEntity> paraMap){
	 logger.info(">>>>>>>> UserController >>>>HystrixCommand fallbackMethod(fallbackGetUserByUnion) handle!");
	 List<UserEntity> ls = new ArrayList<UserEntity>();
	 UserEntity user = new UserEntity();
	 user.setUserName("TestHystrixCommand");
	 ls.add(user);
	 return ls;
    }
}
